package com.housekeeping.housekeeping.advertisingmanagement.controller.productOrder;/*
 * @author  pyh
 * @date  2021/1/23 0023 下午 7:48
 * @version 1.0
 * @since JDK1.8_241
 */

import com.housekeeping.housekeeping.advertisingmanagement.entity.ProductOrder;
import com.housekeeping.housekeeping.advertisingmanagement.service.SearchPOService;
import com.housekeeping.housekeeping.advertisingmanagement.service.impl.SearchPOServiceImpl;
import com.housekeeping.housekeeping.advertisingmanagement.utils.PageUtil;
import com.housekeeping.housekeeping.advertisingmanagement.utils.ResponseCode;
import com.housekeeping.housekeeping.advertisingmanagement.utils.ResponseEntitys;
import com.housekeeping.housekeeping.advertisingmanagement.utils.ResponseUtils;
import com.housekeeping.housekeeping.order.utils.ResponseEntity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

//通过手机电话和状态值联合查询
@WebServlet("/productOrder/statusAndPhone")
public class POSSearchByStatusAId extends HttpServlet {
    private SearchPOService spos = new SearchPOServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //强转为int类型
        int status = Integer.parseInt(req.getParameter("status"));
        String phone = req.getParameter("phone");
        //分页的两个参数page，size
        int page = Integer.parseInt(req.getParameter("page"));
        int size = Integer.parseInt(req.getParameter("size"));
        ResponseEntitys<PageUtil<List<ProductOrder>>> entity = null;
        try {
            //如果电话号码为空则根据状态值查询,反之则根据电话号码和状态值联合查询
            if (phone == null || phone.equals("")){
                PageUtil<List<ProductOrder>> stateList = spos.getStateList(status, page, size);
                entity =ResponseEntitys.success(stateList);
            }else if (phone != null || !phone.equals("")){
                PageUtil<List<ProductOrder>> pAndSList = spos.getPAndSList(phone, status, page, size);
                entity = ResponseEntitys.success(pAndSList);
            }else {
                //双查询失败
                entity = ResponseEntitys.error(ResponseCode.STATE_PHONE_SEARCH_ERROR);
            }
        } catch (SQLException throwables) {
            entity = ResponseEntitys.error(ResponseCode.STATE_PHONE_SEARCH_ERROR);
        }
        ResponseUtils.resp(resp,entity);
    }
}
